<style>

</style>

<div>
    @foreach($bill as $owner)
        @if(isset($owner->firstNotPayItem) && $owner->firstNotPayItem->type == \App\Models\OwnerItem::TYPE_EXCEED)
            <p style="color: red; font-size: 18px;">( 当前账单的所属合同为“时间不标准合同”，请选择“合同续约” 或者点击 “结束合同”)</p> @endif
        @if(isset($owner->firstNotPayItem) && !$owner->firstNotPayItem->other_amount) <p
                style="color: blue; font-size: 18px;">( 请抄表 ) </p> @endif
        @if(isset($owner->firstNotPayItem) && $owner->lastItem->count === $owner->firstNotPayItem->count && $owner->lastItem->type == \App\Models\OwnerItem::TYPE_DEFAULT)
            <p style="color: green; font-size: 18px;">( 当前账单的所属合同为最后一期，如需续约请点击“合同续约” )</p> @endif
        @if($owner->will_close) <p style="color: red; font-size: 18px;">( 已启动到期自动关闭合同功能 )</p> @endif

        <div class="card">
            <div class="card-header">
                <h2>{{ $owner['user']['name'] }} - 流水单号：{{ $owner->firstNotPayItem->id ?? $owner->lastItem->id}}
                    @if($owner['next_rent_day'] >= 0) <span style="font-size:18px; color: green">( 离下次交租还有：{{abs($owner['next_rent_day'])}}天 )</span> @else ( 逾期：{{abs($owner['next_rent_day'])}}天 ) @endif
                </h2>
                <div>
                @if (isset($owner->firstNotPayItem->count) && $owner->lastItem->count === $owner->firstNotPayItem->count)

                    <!-- 最后一期合同 -->
                        @if (!$owner->lastItem->will_close)
                            <button type="button" class="btn btn-primary btn-renew" data-id="{{$owner['id']}}">
                                合同续约
                            </button>
                        @endif

                    <!-- 不是最后一期合同 -->
                        @if ($owner->lastItem->type == \App\Models\OwnerItem::TYPE_EXCEED)
                            <button type="button" class="btn btn-danger btn-finish" data-id="{{$owner['id']}}"
                                    data-toggle="modal"
                                    data-target="#openFinish{{$owner['id']}}">
                                结束合同
                            </button>
                        @else
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#openMeter{{$owner['id']}}">
                                账单抄表
                            </button>
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#staticBackdrop{{$owner['id']}}">
                                杂费填写
                            </button>
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#openOnce{{$owner['id']}}">
                                附加收费
                            </button>
                        @endif
                    @else
                    <!-- 本合同已经全部支付了合同 -->
                        @if($owner['config'] && !$owner->will_close)
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#openMeter{{$owner['id']}}">
                                账单抄表
                            </button>
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#staticBackdrop{{$owner['id']}}">
                                杂费填写
                            </button>
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#openOnce{{$owner['id']}}">
                                附加收费
                            </button>
                        @endif
                    @endif
                    <a target="_blank"
                       href="{{route('admin.pending-bill.pdf', ['bill' => $owner->firstNotPayItem['id'] ?? $owner->lastItem->id])}}"
                       type="button" class="btn btn-primary">打印账单</a>
                </div>
            </div>
            <div class="card-body">
                <table class="table table-bordered">
                    <tbody>
                    <th colspan="7">
                        <a type="button" class="btn btn-primary"
                           href="{{route('admin.running-water.history-bill', ['owner' => $owner['id']])}}">历史流水</a>
                        <span style="font-size: 16px; margin: 0 15px">收租日期：{{ \Carbon\Carbon::parse($owner->firstNotPayItem['started_at'] ?? $owner->lastItem->started_at)->toDateString() }}</span>
                        @if(isset($owner->firstNotPayItem) && $owner->lastItem->count !== $owner->firstNotPayItem->count)
                            <button type="button" class="btn btn-success btn-pay" data-id="{{$owner->id}}"
                                    @if(empty($owner->firstNotPayItem->other_amount)) disabled @endif
                                    data-url="{{route('admin.running-water.pay', ['bill' => $owner->firstNotPayItem['id']])}}">
                                确认收款
                            </button>
                        @elseif(isset($owner->firstNotPayItem) && $owner->lastItem->count === $owner->firstNotPayItem->count && $owner->lastItem->type == \App\Models\OwnerItem::TYPE_DEFAULT)
                            <button type="button" class="btn btn-success btn-pay" data-id="{{$owner->id}}"
                                    @if(empty($owner->firstNotPayItem->other_amount)) disabled @endif
                                    data-url="{{route('admin.running-water.pay', ['bill' => $owner->firstNotPayItem['id'] ?? $owner->lastItem->id])}}">
                                确认收款 并 结束合同
                            </button>
                        @endif
                        <span style="font-size: 16px;margin: 0 10px">账单周期：{{ \Carbon\Carbon::parse($owner->firstNotPayItem['started_at'] ?? $owner->lastItem->started_at )->toDateString() }} 至 {{ \Carbon\Carbon::parse($owner->firstNotPayItem['ended_at'] ?? $owner->lastItem->ended_at )->toDateString() }}</span>
                    </th>
                    @if($owner['config'])
                        <tr>
                            <td>抄表费用项</td>
                            <td>上期表读数</td>
                            <td>本期表读数</td>
                            <td>用量</td>
                            <td>单价</td>
                            <td>金额</td>
                            <td>抄表日期</td>
                        </tr>

                        @foreach($owner['config'] as $key => $value)
                            @if($value['type'] == \App\Models\HouseFeeConfig::TYPE_METER_READING)
                                <!-- 存在最新一期,同时已经抄表 -->
                                @if(isset($owner->firstNotPayItem) && $owner->firstNotPayItem->other_amount)
                                    @foreach($owner->firstNotPayItem->other_amount as $value2)
                                        @if($value2['house_fee_config_id'] == $value['house_config_id'])
                                            <tr>
                                                <td>{{ $value['name'] }}</td>
                                                <td>{{ $value['last_meter']['value'] . $value['unit']}}</td>
                                                <td>{{ $owner->firstNotPayItem->other_amount[$key]['now_meter'] . $value['unit'] ?? "未填" }}</td>
                                                <td>{{ ($owner->firstNotPayItem->other_amount[$key]['reading_num'] . $value['unit']) ?? "未填" }}</td>
                                                <td>{{ $value['price'] }}元/{{ $value['unit'] }}</td>
                                                <td>{{ $owner->firstNotPayItem->other_amount[$key]['fee'] ?? "未填" }}</td>
                                                <td>{{ $owner->updated_at }}</td>
                                            </tr>
                                        @endif
                                    @endforeach

                                    <!-- 最后一期 同时已经支付 -->
                                @elseif($owner->items_count === $owner->lastItem->count && $owner->lastItem->total_amount)
                                    @foreach($owner->lastItem->other_amount as $value2)
                                        @if($value2['house_fee_config_id'] == $value['house_config_id'])
                                            <tr>
                                                <td>{{ $value['name'] }}</td>
                                                <td>{{ $value2['last_meter'] . $value['unit']}}</td>
                                                <td>{{ $owner->lastItem->other_amount[$key]['now_meter'] . $value['unit'] ?? "未填" }}</td>
                                                <td>{{ ($owner->lastItem->other_amount[$key]['reading_num'] . $value['unit']) ?? "未填" }}</td>
                                                <td>{{ $value['price'] }}元/{{ $value['unit'] }}</td>
                                                <td>{{ $owner->lastItem->other_amount[$key]['fee'] ?? "未填" }}</td>
                                                <td>{{ $owner->updated_at }}</td>
                                            </tr>
                                        @endif
                                    @endforeach
                                @elseif(isset($owner->firstNotPayItem) && !$owner->firstNotPayItem->other_amount)
                                    <tr>
                                        <td>{{ $value['name'] . $value['unit']}}</td>
                                        <td>{{ $value['last_meter']['value'] . $value['unit'] }}</td>
                                        <td>未填</td>
                                        <td>未填</td>
                                        <td>{{ $value['price'] }}元/{{ $value['unit'] }}</td>
                                        <td>未填</td>
                                        <td>未填</td>
                                    </tr>
                                @endif
                            @endif
                        @endforeach
                    @endif
                    <tr>
                        <td colspan="7">
                            @if($owner['config'])
                                @foreach($owner['config'] as $value)
                                    @if($value['type'] == \App\Models\HouseFeeConfig::TYPE_CYCLE)
                                        {{ $value['name'] }}：{{ $value['price'] }}/期 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    @endif
                                @endforeach
                            @endif

                            @if(isset($owner->firstNotPayItem) && $owner->firstNotPayItem->once)
                                @foreach($owner->firstNotPayItem->once as $value)
                                    {{ $value['name'] }}：{{ $value['fee'] }}元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                @endforeach
                            @endif

                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="card-footer text-muted" style="display: flex; justify-content: space-between;font-size: 16px">
                <div>
                    <span style="color: #333333; font-weight: bold;margin-right: 15px;">账单租金：{{$owner['cycle_price']}}/期</span>
                    <span style="color: #333333; font-weight: bold;">押金：{{$owner['deposit']}}</span>
                </div>
                @if(isset($owner->firstNotPayItem))
                    <div style="color: #333333; font-weight: bold;">本期账单总金额：<span
                                style="color: #ff8329">{{$owner->firstNotPayItem['total_amount'] ?: $owner->firstNotPayItem['rent']}}</span>
                    </div>
                @else
                    <div style="color: #333333; font-weight: bold;">本期账单总金额：<span
                                style="color: #ff8329">{{$owner->lastItem['total_amount'] ?: $owner->lastItem['rent']}}</span>
                    </div>
                @endif
            </div>
        </div>

        <!-- 杂费填写模态框 -->
        @if($owner['config'])
            <div class="modal fade" id="staticBackdrop{{$owner['id']}}" data-backdrop="static" data-keyboard="false"
                 tabindex="-1"
                 role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="staticBackdropLabel">杂费填写（抄表功能实现后，本功能将废除）</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <form action="{{ route('admin.running-water.update-other-amount', ['bill' => $owner->firstNotPayItem['id'] ?? $owner->lastItem->id])}}"
                              method="post">
                            {{ csrf_field() }}
                            <div class="modal-body">
                                <div class="alert alert-primary" role="alert">请注意：当前为测试版，还没有抄表功能，所以这里直接填写本周期各项杂项的价格
                                </div>
                                <div>

                                    @foreach($owner['config'] as $value)
                                        @if($value['type'] == \App\Models\HouseFeeConfig::TYPE_METER_READING)
                                            <div class="input-group mb-3">
                                                <div class="input-group-prepend">
                                                    <span class="input-group-text">请输入本期{{$value['name']}}费（{{$value['price']}}元/{{$value['unit']}}）</span>
                                                </div>
                                                <input type="text" name="{{$value['house_config_id']}}"
                                                       class="form-control"
                                                       aria-label="Amount (to the nearest dollar)">
                                                <div class="input-group-append">
                                                    <span class="input-group-text">￥</span>
                                                </div>
                                            </div>
                                        @endif
                                    @endforeach
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        @endif

    <!-- 结束合同 -->
        <div class="modal fade" id="openFinish{{$owner['id']}}" data-backdrop="static" data-keyboard="false"
             tabindex="-1"
             role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel">结束合同</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form action="{{ route('admin.finish.update', ['owner' => $owner->id])}}" method="post">
                        {{ csrf_field() }}
                        @method("PUT")
                        <div class="modal-body">
                            <div class="alert alert-primary" role="alert">
                                时间不标准合同（即：合同周期不为整数）必须手动输入最后一期的账单金额（最后一期金额可以包含：租金，一次性收费）。<br/><br/>
                                例子：<br/>
                                1. 合同开始为2020-7-20，合同结束为2020-8-20：这是一个时间标准合同<br/>
                                2. 合同开始为2020-7-20，合同结束为2020-8-25：这是一个时间不标准合同
                            </div>
                            <div>
                                <div class="input-group mb-3">
                                    @if($owner['config'])
                                        <table class="table">
                                            <thead>
                                            <tr>
                                                <th scope="col">名称</th>
                                                <th scope="col">上期表读数</th>
                                                <th scope="col">本期表读数(最后一期抄表)</th>
                                                <th scope="col">金额</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            @foreach($owner->config as $key => $value)
                                                @if($value['type'] == \App\Models\HouseFeeConfig::TYPE_METER_READING)
                                                    @if(empty($value['last_meter']))
                                                        <tr>
                                                            <td colspan="4">数据异常！上期表读数不能为空。请联系开发者</td>
                                                        </tr>
                                                    @else
                                                        <tr>
                                                            <td>{{ $value['name'] }}表</td>
                                                            <td>{{ $value['last_meter']['value'] }}</td>
                                                            <td>
                                                                <label>
                                                                    <input name="{{$value['house_config_id']}}" value="{{$owner->firstNotPayItem->other_amount[$key]['now_meter'] ?? $value['last_meter']['value'] }}">
                                                                </label>
                                                            </td>
                                                            <td>{{$owner->firstNotPayItem->other_amount[$key]['now_meter'] ?? 0 }}</td>
                                                        </tr>
                                                    @endif
                                                @endif
                                            @endforeach
                                            </tbody>
                                        </table>
                                    @endif
                                </div>

                                <div class="form-group row form-field ">
                                    <div for="form-field-total_amount-GW3gd" class="col-md-2  text-capitalize control-label">
                                        <span>最后一期金额</span>
                                    </div>
                                    <div class="col-md-8">
                                        <div class="help-block with-errors"></div>
                                        <div class="input-group">
                                            <span class="input-group-prepend"><span class="input-group-text bg-white"><i class="fa fa-terminal fa-fw"></i></span></span>
                                            <input type="text" id="form-field-total_amount-GW3gd" name="total_amount" value="" class="form-control field_total_amount" placeholder="输入 最后一期金额">
                                        </div>
                                        <span class="help-block"><i class="fa feather icon-help-circle"></i>&nbsp;时间不标准合同最后一期需要手动输金额。 金额：包含租金，一次性收费，周期性付费。<br>&nbsp;&nbsp;&nbsp;&nbsp;注：只有抄表计费由系统计算。意思是：管理员填写时不需要算抄表的费用，系统会给你加上去的，但是其他统统都要管理员算</span>
                                    </div>
                                </div>

                                <div class="form-group row form-field" style="margin-left:30px">
                                    <div class="custom-control custom-checkbox mr-sm-2">
                                        <input type="checkbox" class="custom-control-input" id="customControlAutosizing" name="remove_deposit" checked>
                                        <label class="custom-control-label" for="customControlAutosizing">是否在本次收费中扣除押金？</label>
                                    </div>
                                </div>

                                <div class="form-group row" style="margin: 0 20px">
                                    <div class="alert alert-info alert-dismissable">
                                        <i class="fa fa-exclamation-circle"></i> 点击提交（结束合同）后，如果还未到合同结束时间，系统不会立即结束合同，系统会等到合同结束时间后再结束合同。
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                            <button type="submit" class="btn btn-danger">结束合同</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- 抄表模态框 -->
        @if($owner['config'])
            <div class="modal fade" id="openMeter{{$owner['id']}}" data-backdrop="static" data-keyboard="false"
                 tabindex="-1"
                 role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="staticBackdropLabel">账单抄表</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <form action="{{ route('admin.running-water.update-meter', ['bill' => $owner->firstNotPayItem['id'] ?? $owner->lastItem->id])}}"
                              method="post">
                            {{ csrf_field() }}
                            <div class="modal-body">
                                <div class="alert alert-primary" role="alert">金额 = (本期表读数 - 上期表读数) * 单价</div>
                                <div>
                                    <div class="input-group mb-3">
                                        <table class="table">
                                            <thead>
                                            <tr>
                                                <th scope="col">名称</th>
                                                <th scope="col">上期表读数</th>
                                                <th scope="col">本期表读数</th>
                                                <th scope="col">金额</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            @foreach($owner->config as $key => $value)
                                                @if($value['type'] == \App\Models\HouseFeeConfig::TYPE_METER_READING)
                                                    @if(empty($value['last_meter']))
                                                        <tr>
                                                            <td colspan="4">数据异常！上期表读数不能为空。请联系开发者</td>
                                                        </tr>
                                                    @else
                                                        <tr>
                                                            <td>{{ $value['name'] }}表</td>
                                                            <td>{{ $value['last_meter']['value'] }}</td>
                                                            <td>
                                                                <label>
                                                                    <input name="{{$value['house_config_id']}}"
                                                                           value="{{$owner->firstNotPayItem->other_amount[$key]['now_meter'] ?? $value['last_meter']['value'] }}">
                                                                </label>
                                                            </td>
                                                            <td>{{$owner->firstNotPayItem->other_amount[$key]['now_meter'] ?? 0 }}</td>
                                                        </tr>
                                                    @endif
                                                @endif
                                            @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        @endif

    <!-- 附加收费项 -->
        <div class="modal fade" id="openOnce{{$owner['id']}}" data-backdrop="static" data-keyboard="false"
             tabindex="-1"
             role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="staticBackdropLabel">附加收费</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form action="{{ route('admin.running-water.update-once', ['bill' => $owner->firstNotPayItem['id'] ?? $owner->lastItem->id])}}"
                          method="post">
                        {{ csrf_field() }}
                        <div class="modal-body">
                            <div class="alert alert-primary" role="alert">附加收费简单来说就是：增加本周期的收费</div>
                            <div class="alert alert-primary" role="alert">如果配置项没有选项：请到房产管理 -> 公共收费项处，新增类型为“固定收费”的收费项目，<a
                                        href="{{route("admin.house-fee-config.create")}}" style="color: red">点我快速添加</a>
                            </div>

                            <!-- 附加收费start -->
                            <div class="row form-group">
                                <div class="col-md-12">
                                    <div class="help-block with-errors"></div>
                                    <span name="config"></span>
                                    <div id="has-many-config">
                                        <table class="table table-has-many has-many-config">
                                            <thead>
                                            <tr>
                                                <th>配置项id</th>
                                                <th>金额</th>
                                            </tr>
                                            </thead>
                                            <tbody class="has-many-config-forms">
                                            @if(isset($owner->firstNotPayItem) && $owner->firstNotPayItem->once)
                                                @foreach($owner->firstNotPayItem->once as $item)
                                                    <tr class="has-many-config-form fields-group">
                                                        <td>
                                                            <div class="col-md-12">
                                                                <select class="form-control" style="width: 100%;"
                                                                        name="ids[]">
                                                                    <option value="0"></option>
                                                                    @foreach($once as $value)
                                                                        <option @if($item['house_fee_config_id'] == $value->id) selected="selected"
                                                                                @endif value="{{$value->id}}">{{$value->name}}
                                                                            (每/{{$value->unit}})
                                                                        </option>
                                                                    @endforeach
                                                                </select>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="input-group">
                                                                <span class="input-group-prepend"><span
                                                                            class="input-group-text bg-white"><i
                                                                                class="fa fa-terminal fa-fw"></i></span></span>
                                                                <input required="1" type="text" name="prices[]"
                                                                       value="{{$item['fee']}}"
                                                                       class="form-control field_cycle_price"
                                                                       placeholder="输入 金额">
                                                            </div>
                                                        </td>
                                                        <td class="form-group">
                                                            <div>
                                                                <div class="remove btn btn-white btn-sm pull-right"><i
                                                                            class="feather icon-trash"></i></div>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                @endforeach
                                            @endif
                                            </tbody>
                                        </table>
                                        <div class="form-group row m-t-10">
                                            <div class="col-md-8" style="margin-top: 8px">
                                                <div class="add btn btn-success btn-sm">
                                                    <i class="feather icon-plus"></i>&nbsp;新增
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- 附加收费end -->
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        {{--            @endif--}}
    @endforeach
    {{$bill->links()}}
</div>

<script>
    Dcat.ready(function () {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        // 确认收款按钮
        $('.btn-pay').click(function () {
            let url = $(this).data('url');
            let id = $(this).data('id');
            Dcat.confirm('是否确认收款？', null, () => {
                $.ajax({
                    url: url,
                    method: "post",
                    contentType: "application/json",
                    data: JSON.stringify({
                        _token: "{{ csrf_token() }}"
                    }),
                }).then((res) => {
                    if (res.code === 200) {
                        Dcat.swal.success(res.message).then(function () {
                            location.reload();
                        });
                    } else {
                        Dcat.swal.error(res.message, '').then(function () {
                            location.reload();
                        });
                    }
                }).catch((err) => {
                    if (err.status === 424) {
                        swal("当前合同已经是最后一期收款，是否需要续约合同?", {
                            buttons: {
                                cancel: "取消",
                                finish: {
                                    text: "结束合同",
                                    value: "finish",
                                },
                                renew: {
                                    text: "续约合同",
                                    value: "renew",
                                },
                            },
                        }).then((res) => {
                            // 根据不同的选择跳转到相应的界面
                            if (res === "finish") {
                                // 跳转到结束合同界面，
                            } else if (res === "renew") {
                                // 跳转到续约界面
                                location.href = "/admin/renew/" + id + "/edit";
                            }
                        });
                    } else if (err.status.startWith(4)) {
                        Dcat.swal.error(err.msg, '');
                    } else {
                        Dcat.swal.error('服务器异常，请联系管理员', '');
                    }
                })
            });
        });

        // 合同续约
        $('.btn-renew').click(function () {
            let id = $(this).data('id');
            location.href = "/admin/renew/" + id + "/edit";
        });

        // 结束合同
        // $('.btn-finish').click(function () {
        //     let id = $(this).data('id');
        //     location.href = "/admin/finish/" + id + "/edit";
        // })

        // 添加按钮
        $(".add").click(function () {
            let html = '<tr class="has-many-config-form fields-group">'
                + '<td>'
                + '<div class="col-md-12">'
                + '<select class="form-control" style="width: 100%;" name="ids[]">'
                + '<option value="0"></option>'
                    @foreach($once as $value)
                + '<option value="{{$value->id}}">{{$value->name}}(每/{{$value->unit}})</option>'
                    @endforeach
                + '</select>'
                + '</div>'
                + '</td>'
                + '<td>'
                + '<div class="input-group">'
                + '<span class="input-group-prepend"><span class="input-group-text bg-white"><i class="fa fa-terminal fa-fw"></i></span></span>'
                + '<input required="1" type="text" name="prices[]" value="" class="form-control field_cycle_price" placeholder="输入 金额">'
                + '</div>'
                + '</td>'
                + '<td class="form-group">'
                + '<div>'
                + '<div class="remove btn btn-white btn-sm pull-right"><i class="feather icon-trash"></i></div>'
                + '</div>'
                + '</td>'
                + '</tr>';
            $(".has-many-config-forms").append(html);
        });

        // 移除配置项
        $(document).on('click', '.remove', function () {
            $(this).parent().parent().parent().remove();
        });
    });
</script>
